﻿/*
Copyright 2011 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 Authors:
 Nadim Ratani      (nratani@google.com)
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Newtonsoft.Json.Linq;

namespace Sample.Controllers
{
    /// <summary>
    /// The controller for the receipt page, where the user is shown a
    /// receipt for their purchase.
    /// </summary>
    public class ReceiptController : Controller
    {
        /// <summary>
        /// Decodes the full wallet response supplied by the API.
        /// Creates a transaction status notification JWT.
        /// </summary>
        public ActionResult Index()
        {
            // Decode and verify the full wallet response JWT
            string fullWalletResponseString = JWT.JsonWebToken.Decode(Request["fullWalletJWT"], Config.signingKey);
            JObject fullWalletResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(fullWalletResponseString);

            DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            TimeSpan diff = DateTime.UtcNow - origin;
            int iat = (int)Math.Floor(diff.TotalSeconds);

            JObject transactionStatusNotification = new JObject(
                new JProperty("iss", Config.iss),
                new JProperty("aud", Config.aud),
                new JProperty("iat", iat),
                new JProperty("google_transaction_id", fullWalletResponse.SelectToken("google_transaction_id")),
                new JProperty("typ", "google/paymentexpress/v1/transactionstatus"),
                new JProperty("status", "SUCCESS")
            );
            string transactionStatusNotificationJWT = JWT.JsonWebToken.Encode(transactionStatusNotification, Config.signingKey, JWT.JwtHashAlgorithm.HS256);

            ViewData["transactionStatusNotificationJWT"] = transactionStatusNotificationJWT;


            ViewData["name"] = fullWalletResponse.SelectToken("selection.pay.payment_instrument.billing_address.name");
            ViewData["cardName"] = fullWalletResponse.SelectToken("selection.pay.payment_instrument.descriptive_name");
            ViewData["type"] = fullWalletResponse.SelectToken("selection.pay.payment_instrument.type");
            ViewData["expirationMonth"] = fullWalletResponse.SelectToken("selection.pay.payment_instrument.expiration_month");
            ViewData["expirationYear"] = fullWalletResponse.SelectToken("selection.pay.payment_instrument.expiration_year");

            return View();
        }
    }
}
